package tech.aistar.controller.center;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.*;
import tech.aistar.service.center.IAddressService;
import tech.aistar.service.center.IRealNameService;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.service.comment.IAnswerService;
import tech.aistar.service.comment.ICommentService;
import tech.aistar.service.demand.IPictureService;
import tech.aistar.service.services.IServicesListService;
import tech.aistar.service.services.IServicesService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 23827 on 2019/9/29.
 */
@Controller
@RequestMapping("/center")
@SuppressWarnings("all")
public class CenterController {

    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IAddressService iAddressService;
    @Autowired
    private IServicesListService iServicesListService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IRealNameService iRealNameService;
    @Autowired
    private IServicesService iServicesService;
    @Autowired
    private IPictureService iPictureService;

    @GetMapping("/list")
    public String center(Model model, Integer id, HttpSession session){

        //个人信息
        //     System.out.println(session.getAttribute("userinfoId"));
        Integer uid = (Integer) session.getAttribute("userinfoId");
        UserInfo userInfo = iUserInfoService.ById(uid);
        System.out.println("=================");
        System.out.println(uid);
        userInfo.getAddressId();
        System.out.println(userInfo.getAddressId());
//        Address address = iAddressService.findById( uid);
        List<Address> address = iAddressService.findAllById(uid);
        System.out.println(address);
        RealName realName = iRealNameService.findByUserId(uid);
        Picture p = new Picture();
        p.setId(userInfo.getId());
        Picture picture = iPictureService.findPathById(userInfo.getId());

//     System.out.println("userInfo = " + userInfo);
        session.setAttribute("userinfo",userInfo);
        session.setAttribute("address",address);
        session.setAttribute("realName",realName);
        session.setAttribute("picture",picture);






        //评论
        //评论
        List<Comment> commentList = commentService.comFindser(2);
//        List<Comment> commentList = commentService.comCounts(id);
        //获取评论人的id
        List<UserInfo> userInfos = new ArrayList<>();
//
        for (int i = 0; i < commentList.size(); i++) {
            System.out.println(commentList.get(i).getUserinfoId());
            //获取userinfoId 评论的姓名
            UserInfo userInfo1 = userInfoService.findById(commentList.get(i).getUserinfoId());
            userInfo1.setId(commentList.get(i).getUserinfoId());
            userInfos.add(userInfo1);
        }

        Integer userinfo4 = (Integer) session.getAttribute("userinfoId");

        List<Answer> answer1 = answerService.userId(userinfo4);

        //放入作用域
        model.addAttribute("answer1",answer1);
        model.addAttribute("commentList",commentList);
        model.addAttribute("userInfos",userInfos);

        return "user/center";
    }

    @RequestMapping("/addanswer")
    public String answer(HttpServletRequest request, HttpSession session) {
        //获取评论的内容
        String content = request.getParameter("cm");
        //获取user的id
        Integer userinfo = (Integer) session.getAttribute("userinfoId");

        Answer answer = new Answer();

        //时间类型的转换
//        Date currentTime = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String dateString = formatter.format(currentTime);

        //数据的存储
        answer.setStatus(0);
        answer.setTime(new Date());
        answer.setContent(content);
        answer.setUserinfoId(userinfo);

        answerService.anSave(answer);
        return "redirect:/center/list";

    }

//    @RequestMapping("service")
//    public String service(HttpSession session){
//        Integer uid = (Integer) session.getAttribute("userinfoId");
//        List<Services> servicesList = iServicesService.findByUserInfoId(uid);
//
//        UserInfo userInfo = iUserInfoService.findById(uid);
//        session.setAttribute("userInfo",userInfo);
//        session.setAttribute("servicesList",servicesList);
//        return "user/center";
//    }


}
